package com.atcx.service.impl;

import com.atcx.mapper.AchievementMapper;
import com.atcx.mapper.ClassesMapper;
import com.atcx.mapper.StudentClassMapper;
import com.atcx.mapper.UserMapper;
import com.atcx.pojo.Achievement;
import com.atcx.pojo.Classes;
import com.atcx.pojo.StudentClass;
import com.atcx.pojo.User;
import com.atcx.service.StudentClassService;
import com.atcx.util.PageResult;
import com.atcx.util.QueryPageBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pangShu
 * @since 2022-03-23
 */
@Service
public class StudentClassServiceImpl extends ServiceImpl<StudentClassMapper, StudentClass> implements StudentClassService {


    @Resource
    private AchievementMapper achievementMapper;
    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private StudentClassMapper studentClassMapper;
    @Override
    public int AddStudent(StudentClass studentClass) {
        int insert = studentClassMapper.insert(studentClass);
        if (insert>0){
            Classes classes = classesMapper.selectById(studentClass.getClassid());
            classes.setClassid(studentClass.getClassid());
            classes.setNumber(classes.getNumber()+1);
            classesMapper.updateById(classes);
        }
        return insert;
    }

    @Override
    public int deleteStudent(int id) {
        QueryWrapper<StudentClass> wrapper = new QueryWrapper<>();
        wrapper.eq("userid",id);
        StudentClass studentClass = studentClassMapper.selectOne(wrapper);
        int delete = studentClassMapper.delete(wrapper);
        if (delete>0){
            Classes classes = classesMapper.selectById(studentClass.getClassid());
            classes.setNumber(classes.getNumber()-1);
            classesMapper.updateById(classes);
        }
        return delete;
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Page<StudentClass> studentClassPage = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        QueryWrapper<StudentClass> wrapper = new QueryWrapper<>();
        if (queryPageBean.getQueryString() != null&&queryPageBean.getQueryString().trim().length()!=0){
            //有查询条件就添加查询条件
            String trim = queryPageBean.getQueryString().trim();
            wrapper.eq("claId",trim);
        }
        Page<StudentClass> page = studentClassMapper.selectPage(studentClassPage, wrapper);
        List<StudentClass> studentClassList = page.getRecords();
        for (StudentClass studentClass : studentClassList) {
            //先设置班级信息
            Classes classes = classesMapper.selectById(studentClass.getClassid());
            studentClass.setCalssName(classes.getClassname());

            //再设置学生信息
            User user = userMapper.selectById(studentClass.getUserid());
            studentClass.setName(user.getName());
            studentClass.setSex(user.getSex());
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;
    }

    @Override
    public Integer getClassId(int UserId) {
        QueryWrapper<StudentClass> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",UserId);
        StudentClass studentClass = studentClassMapper.selectOne(wrapper);
        return studentClass.getClassid();
    }


}
